package com.gobo.ucenterservice.controller;


import com.gobo.commonutils.JwtUtils;
import com.gobo.commonutils.R;
import com.gobo.servicebase.handler.exception.pojo.GoboException;
import com.gobo.servicebase.vo.MemberInfoVo;
import com.gobo.ucenterservice.pojo.UcenterMember;
import com.gobo.ucenterservice.pojo.vo.LoginInfoVo;
import com.gobo.ucenterservice.pojo.vo.RegisterMember;
import com.gobo.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/ucenterservice/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 注册接口
     * @param registerMember
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterMember registerMember){
        if (StringUtils.isEmpty(registerMember.getMobile()) ||
            StringUtils.isEmpty(registerMember.getCode()) ||
            StringUtils.isEmpty(registerMember.getNickname()) ||
            StringUtils.isEmpty(registerMember.getPassword()) ){
            return R.error().message("参数非法！");
        }
        Boolean isSave = memberService.saveRegisterMember(registerMember);
        return isSave?R.ok().message("注册成功"):R.error().message("注册失败");
    }
    @PostMapping("/login")
    public R register(@RequestBody UcenterMember ucenterMember){
        if (StringUtils.isEmpty(ucenterMember.getMobile()) ||
                StringUtils.isEmpty(ucenterMember.getPassword()) ){
            return R.error().message("参数非法！");
        }
        String token = memberService.checkMember(ucenterMember);
        return token!=null?R.ok().message("登录成功").data("token",token):R.error().message("登录失败");
    }

    /**
     * 获取用户基本信息
     * @param request
     * @return
     */
    @GetMapping("/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            if (StringUtils.isEmpty(memberId)){
                return R.error().message("用户Id失效");
            }
            LoginInfoVo member = memberService.getLoginInfo(memberId);
            return R.ok().data("item", member);
        }catch (Exception e){
            e.printStackTrace();
            throw new GoboException(20001,"error");
        }
    }
    /**
     * 通过Id  分布式架构 其他模块通用
     * 获取用户基本信息
     * @param id
     * @return
     */
    @GetMapping("/getMemberInfoVo/{id}")
    public MemberInfoVo getMemberInfoVo(@PathVariable String id){
        MemberInfoVo member = memberService.getMemberInfoVo(id);
        return member;
    }


    @PostMapping("countregister/{day}")
    public R registerCount(@PathVariable String day){
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);
    }

}

